#ifndef TASK_THREAD_H
#define TASK_THREAD_H

#include "../noncopyable.h"
#include "../Types.h"
#include "../Atomic.h"
#include "CountDownLatch.h"

#include <pthread.h>
#include <unistd.h>
#include <functional>

class Thread : muduo::noncopyable {
public:
    typedef std::function<void()> ThreadFunc;
    // 线程要执行的方法

    explicit Thread(ThreadFunc, string name = string());

    ~Thread();

    void start();

    int join(); // return pthread_join()

    bool started() const { return started_; }

    // pthread_t pthreadId() const { return pthreadId_; }
    pid_t tid() const { return tid_; }

    const string &name() const { return name_; }

    static int numCreated() { return numCreated_.get(); }

private:
    void setDefaultName();

    bool started_;
    // 线程是否处于启动状态
    bool joined_;
    // 已加入
    pthread_t pthreadId_;
    pid_t tid_;
    ThreadFunc func_;
    // 线程工作的函数
    string name_;
    // 线程名
    CountDownLatch latch_;

    static AtomicInt32 numCreated_;
};


#endif //TASK_THREAD_H
